package com.example.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Constants;
import com.example.mapper.ConfigMapper;
import com.example.pojo.Config;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.util.Optional;

/** mvc配置
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private ConfigTableValueConfig configValue;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //配置允许跨域的路径
        registry.addMapping("/**")
                //配置允许访问的跨域资源的请求域名
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                //配置允许访问该跨域资源服务器的请求方法
                .allowedMethods("*")
                //配置允许请求 头部head的访问
                .allowedHeaders("*");
    }

    /**
     * 设置文件虚拟路径映射
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取资源储存文件夹路径
        String resPath = configValue.getResRootPath();
        // 建立资源路径映射
        if (StringUtils.hasText(resPath)) {
            registry.addResourceHandler(Constants.NET_RES_PATH_PREFIX + "/**").
                    addResourceLocations("file:" + resPath + File.separator);
        }
    }
}